package com.be.beadmin.product.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.product.dto.input.ProductBopInforDto;
import com.be.beadmin.product.dto.output.ProductBopInforEx;
import com.be.beadmin.product.service.ProductBopInforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zjm
* @date 2023-03-19
**/
@RestController
public class ProductBopInforController {

    @Autowired
    private  ProductBopInforService productBopInforService;


    @PostMapping(value = "/product/getProductBopInforByPage")
    public ResultModel getProductBopInforByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(productBopInforService.getProductBopInforByPage(pageEntity)) ;
    }

    @PostMapping("/product/addProductBopInfor")
    public ResultModel addProductBopInfor(@RequestBody ProductBopInforDto productBopInforDto){
        productBopInforService.addProductBopInfor(productBopInforDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/product/delProductBopInfor")
    public ResultModel delProductBopInfor(@RequestBody ProductBopInforDto productBopInforDto){
        productBopInforService.delProductBopInfor(productBopInforDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/product/updateProductBopInfor")
    public ResultModel updateProductBopInfor(@RequestBody ProductBopInforDto productBopInforDto){
        productBopInforService.updateProductBopInfor(productBopInforDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportProductBopInfor")
    public void exportProductBopInfor(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<ProductBopInforEx> productBopInforPage = productBopInforService.getProductBopInforByPage(pageEntity);
        List<ProductBopInforEx> productBopInforExes = productBopInforPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("ProductBopInfor接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ProductBopInforEx.class).sheet("sheet1").doWrite(productBopInforExes);
    }
}